<?php
/**
 * Composing and sending mail to groups and members
 *
 * ACCESS: ADMINS & XALT & Worship ONLY
 *
 * @author Chris Lunsford
 * @version 1.0
 */
class Mail extends Controller {
	
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();	
	}
	
	/******************** URL Functions ********************/
	
	/**
	 * Displays page: Composing Mail -> moved to Mail controller to allow other groups access
	 */
	function composeMail() {
		$data = $this->_buildContent();
		$user = $this->UserModel->getUserByName($this->session->userdata('username'));
		if ($this->uri->segment(3) == "group") {
			//we want to make sure that the person accessing this page is allowed to
			$group = $this->UserModel->getGroup($this->uri->segment(4));
			if ($this->UserModel->isLeader($user->userid, $group->groupid) || $this->badger->checkAllow('admin xalt')) {
				$data['to_type'] = "group";
				$data['to'] = $group;
			} else {
				$this->session->set_flashdata('msg_error', 'The portion of the site you attempted to access is restricted.');
				redirect('site/login');
			}
		} else if ($this->uri->segment(3) == "user") {
			$this->badger->checkAllow('admin xalt');
			$data['to_type'] = "user";
			$data['to'] = $this->UserModel->getUser($this->uri->segment(4));
		} else {
			//this is where the email all feature will reside sometime later...
			redirect('blog');
		}
		
		$data['from'] = $user;
		$this->load->view('mail/mail_compose', $data);
	}
	
	/******************* Task Functions *******************/
	
	/**
	 * Sends an email to specified recipients -> should be moved to mail controller to give other groups access
	 */
	function sendMail() {
		$this->load->library('email');
		$sender = $this->UserModel->getUserByName($this->session->userdata('username'));
		$redirect = 'admin';
		
		$this->email->from($sender->email);
		
		if ($this->input->post('to_type') == 'group') {
			$group = $this->UserModel->getGroupMembers($this->input->post('to'));
			foreach ($group->result() as $row) {
				$email_array[] = $row->user->email;
			}
			
			$this->email->to($email_array);
			
			$redirect = 'groups';
		} else if ($this->input->post('to_type') == 'user') {
			$user = $this->UserModel->getUser($this->input->post('to'));

			$this->email->to($user->email);
			
			$redirect = 'admin/users';
		} else if ($this->input->post('to_type') == 'all') {
			$users = $this->UserModel->getUsers();
			foreach ($users->result() as $row) {
				$email_array[] = $row->email;
			}
			$this->email->to($email_array);
			$redirect = 'admin/users';
		}
		
		$this->email->subject($this->input->post('subject',TRUE));
		$this->email->message($this->input->post('message',TRUE));
		$this->email->send();
		$this->session->set_flashdata('msg_info', 'Your message was sent successfully.');
		
		redirect($redirect);
	}
	
		/*
	 * Supplies user-specific information to the interface for customization
	 */
	function _buildContent() {
		if ($this->session->flashdata('msg_info')) $data['msg_info'] = $this->session->flashdata('msg_info');
		if ($this->session->flashdata('msg_error'))	$data['msg_error'] = $this->session->flashdata('msg_error');
		
		//test if user is logged in for site navigation
		if ($this->session->userdata('loggedin')) {
			$data['loggedin'] = TRUE;
			$this->load->model('UserModel');
			$user = $this->UserModel->getUserByName($this->session->userdata('username'));
			$data['con_user'] = $user;
			
			//is admin?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('admin'))) {
				$data['admin_tab'] = TRUE;
			}
		}
		
		return $data;
	}
}
?>